/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.ruralLibrary.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.ruralLibrary.api.entity.BookUserEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.terrainEntity;
import com.pig4cloud.pigx.ruralLibrary.service.BookUserService;
import com.pig4cloud.pigx.ruralLibrary.service.terrainService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ${tableComment}
 *
 * @author pig
 * @date 2023-06-30 10:08:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/summary")
@Tag(description = "summary", name = "地势管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TerrainController {

	private final terrainService terrainService;

	private final BookUserService bookUserService;

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('ruralLibrary_summary_view')")
	public R gettopographicSummaryPage(Page page, terrainEntity terrainEntity) {
		String name = SecurityUtils.getUser().getUsername();
		LambdaQueryWrapper<BookUserEntity> userwrapper = Wrappers.lambdaQuery();
		userwrapper.eq(BookUserEntity::getUserName, name);
		BookUserEntity bookUserEntity = bookUserService.getOne(userwrapper);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (!bookUserEntity.getJurisdiction().equals("1")&&!bookUserEntity.getJurisdiction().equals("0")) {
				if (bookUserEntity.getJurisdiction().equals("2")) {
					terrainEntity.setCode(bookUserEntity.getCity());
				} else if (bookUserEntity.getJurisdiction().equals("3")) {
					terrainEntity.setCode(bookUserEntity.getDistrict());
				} else if (bookUserEntity.getJurisdiction().equals("4")) {
					terrainEntity.setCode(bookUserEntity.getVillage());
				} else if (bookUserEntity.getJurisdiction().equals("5")) {
					terrainEntity.setCode(bookUserEntity.getCommittee());
				} else {
					return R.failed("该用户无权限");
				}
			}
		} else {
			return R.failed("该用户权限出错");
		}
		return R.ok(page.setRecords(terrainService.getAll(page, terrainEntity, bookUserEntity.getJurisdiction())));
	}


	/**
	 * 通过id查询${tableComment}
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('ruralLibrary_summary_view')")
	public R getById(@PathVariable("id") String id) {
		return R.ok(terrainService.getOne(id));
	}

	/**
	 * 新增${tableComment}
	 *
	 * @param topographicSummary ${tableComment}
	 * @return R
	 */
	@Operation(summary = "新增居委会", description = "新增居委会")
	@SysLog("新增${tableComment}")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('ruralLibrary_summary_add')")
	public R save(@RequestBody terrainEntity topographicSummary) {
		return terrainService.insert(topographicSummary);
	}

	/**
	 * 修改${tableComment}
	 *
	 * @param topographicSummary ${tableComment}
	 * @return R
	 */
	@Operation(summary = "修改居委会", description = "修改居委会")
	@SysLog("修改${tableComment}")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('ruralLibrary_summary_edit')")
	public R updateById(@RequestBody terrainEntity topographicSummary) {
		return R.ok(terrainService.updateCommitteeName(topographicSummary));
	}

	/**
	 * 通过id删除${tableComment}
	 *
	 * @return R
	 */
	@Operation(summary = "通过id删除", description = "通过id删除")
	@SysLog("通过id删除${tableComment}")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('ruralLibrary_summary_del')")
	public R removeById(@RequestBody String id) {
		return terrainService.removeBatchByIds(id);
	}

	/**
	 * 导出excel 表格
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('ruralLibrary_summary_export')")
	public List<terrainEntity> export(terrainEntity terrainEntity) {
		return terrainService.export(terrainEntity);
	}

	@Operation(summary = "获取市", description = "获取市")
	@Inner(value = false)
	@GetMapping(value = "/getCity")
	public R getCity() {
		return terrainService.getCity();
	}

	@Operation(summary = "根据市获取区县", description = "根据市获取区县")
	@Inner(value = false)
	@GetMapping(value = "/getDistrict/{city}")
	public R getDistrict(@PathVariable("city") String city) {
		return terrainService.getDistrict(city);
	}

	@Operation(summary = "根据区县获取街道", description = "根据区县获取街道")
	@Inner(value = false)
	@GetMapping(value = "/getVillage/{district}")
	public R getVillage(@PathVariable("district") String district) {
		return terrainService.getVillage(district);
	}

	@Operation(summary = "根据街道获取居委会", description = "根据街道获取居委会")
	@Inner(value = false)
	@GetMapping(value = "/getCommittee/{committee}")
	public R getCommittee(@PathVariable("committee") String committee) {
		return terrainService.getCommittee(committee);
	}

	@Operation(summary = "获取居委会全部名称", description = "获取居委会全部名称")
	@Inner(value = false)
	@GetMapping(value = "/getAll/{id}")
	public String getAll(@PathVariable("id") String id) {
		return terrainService.getAll(id);
	}

	@Inner
	@GetMapping("/getOne/{id}")
	public terrainEntity getOne(@PathVariable("id") String id) {
		return terrainService.getOne(id);
	}
}
